package com.algorithm.ch1.cjm.linkedlist.model;

import java.util.Objects;

/**
 * 链表对象定义
 */
public class ListNode {

    public int val;

    public ListNode next;

    public ListNode(int x){
        val = x;
    }

    public static void print(ListNode head){

        if(head == null){
            return;
        }

        ListNode nodeA = head;

        StringBuffer sb = new StringBuffer();
        while (nodeA != null){

            sb.append(nodeA.val + ",");
            nodeA = nodeA.next;
        }

        String str = sb.substring(0, sb.length() -1);

        System.out.println(str);
    }

    /**
     * 反转
     * @param head
     * @return
     */
    public static ListNode reverseList(ListNode head){
        ListNode pre = null;
        ListNode next = head;

        while (next != null){
            ListNode temp = next.next;
            next.next = pre;
            pre = next;
            next = temp;
        }
        return pre;
    }
}
